package com.wwh.tx.homework.service;

import com.wwh.tx.homework.dao.BuyerDAO;
import com.wwh.tx.homework.dao.GoodDAO;
import com.wwh.tx.homework.dao.SellerDAO;
import com.wwh.tx.homework.dao.TaobaoDAO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Albert
 * @date 2024/8/29-15:44
 * @describe
 */
@Service
public class ShopService {
    @Resource
    private SellerDAO sellerDAO;
    @Resource
    private BuyerDAO buyerDAO;
    @Resource
    private GoodDAO goodDAO;
    @Resource
    private TaobaoDAO taobaoDAO;

    /**
     * 购买商品
     * @param buyerId 买家id
     * @param goodId 商品id
     * @param num 商品数量
     */
    @Transactional
    public void buy(int buyerId, int goodId, int num){
        //查询商品单价
        double price = goodDAO.queryPriceById(goodId);
        //计算总价钱
        double totalPrice = price * num;
        //买家账户扣钱
        buyerDAO.updateMoney(buyerId, totalPrice);
        //淘宝抽成
        taobaoDAO.updateMoney(totalPrice * 0.1);
        //卖家账户入账
        sellerDAO.updateMoney(goodDAO.querySellerIdById(goodId), totalPrice * 0.9);
        //修改商品库存
        goodDAO.updateStock(goodId, num);
    }
}
